package com.study.sort;

/**
 * 冒泡排序以及优化
 */
public class BubbleSort {

    public static void main(String[] args) {
        int arr[] = {9, 5, 4, 8, 6,2,-5,93,};
        arr = bubbleSort(arr);
        for (int i : arr) {
            System.out.println(i);
        }
    }


    public static int[] bubbleSort(int[] arr) {
        boolean flag = true;

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
        return arr;
    }
}
